package com.xgxd.modules.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xgxd.commons.utils.*;
import com.xgxd.modules.entity.YmlGet;
import com.xgxd.modules.entity.YmlPut;
import com.xgxd.modules.servcie.EirService;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.beans.PropertyDescriptor;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping(value = "api")
public class Eir {

    @Autowired
    private EirService eirService;

    private final String SUCCESS = "S";

    private final String FAIL = "E";

    /**
     * 获取EIR数据接口
     *
     * @param bknbr 关单号
     * @return
     */

    @GetMapping(value = {"yml_get/{bknbr}"})
    public String ymlGet(@PathVariable("bknbr") String bknbr) {
        return eirService.ymlGet(bknbr);
    }

    @GetMapping(value = {"yml_get_json/{bknbr}"})
    public String ymlGetJson(@PathVariable("bknbr") String bknbr) {
        return eirService.ymlGetJson(bknbr);
    }

    /**
     * 提交出口EIR打印记录 new
     *
     * @param id 实体
     * @return
     */
    @RequestMapping(value = {"yml_put"})
    public String yml_put(String id) {
        System.out.println("调用put id " + id + "---------start sign");
        String uuid = SqenUtils.uuid();
        if (eirService.get(id).size() == 0) {
            YmlPut ymlPut = new YmlPut();
            ymlPut.setIsSuccess(FAIL);
            ymlPut.setTryMessage("未查询到记录");
            ymlPut.setUuid(uuid);
            //调用方id
            ymlPut.setYmsald(id);
            eirService.saveYmlput(ymlPut);
            return uuid;
        } else {
            System.out.println("找到400记录开始调用-------eirService.ymlPut----- " + id + "---------start sign");
            return eirService.ymlPut(id);
        }
    }


    @RequestMapping(value = {"saveError"})
    public String saveError(String id, String errMsg) {
        return eirService.saveError(id, errMsg);
    }


    @RequestMapping(value = {"comparison"})
    public String comparison() {
        List<Map<String, Object>> waitList =eirService.getWaait();

        for (int i = 0; i < waitList.size(); i++) {
            try{
                String id = waitList.get(i).get("YMSEQN").toString().trim();
                eirService.ymlPut(id);

            }catch (Exception e){
                System.out.println(i+"个 ---id为====errrr");
            }
            System.out.println("maxList--size:"+i);
        }

        return "";
    }

    @RequestMapping(value = {"info"})
    public String info() {
        return "ok";
    }
}

